
package com.tansun.ruledata.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tansun.rule.redis.RedisService;
import com.tansun.rule.utils.DroolsRuleLog;
import com.tansun.ruledata.data.entity.DeployRuleLogs;
import com.tansun.ruledata.data.service.RuleLogBackupService;
import com.tansun.ruledata.entity.RuleLogDetails;
import com.tansun.ruledata.service.RuleLogService;


@Service("ruleLogService")
public class RuleLogServiceImpl implements RuleLogService {
	@Autowired
	private RedisService redisService;
	@Autowired
	private RuleLogBackupService ruleLogService;
	public void saveRuleLog(DeployRuleLogs ruleLogs) {
		String ruleLogStr = "";
		List<String> ruleLogDetail = new ArrayList<String>();
		try {
			Date currDate = new Date();
			String[] ruleflowInfo = ruleLogs.getRuleflowid().split("_");
			ruleLogs.setRuleflowCode(ruleflowInfo[0]);
			//ruleLogs.setVersion(ruleflowInfo[1]);
			ruleLogs.setCreateDate(currDate);
			ruleLogStr = JSON.toJSONString(ruleLogs);
			List<Map<String, Object>> ruldLog = ruleLogs.getLogs();
			for (Map<String, Object> map : ruldLog) {
				RuleLogDetails details = new RuleLogDetails();
				if(map.get("group")==null)
					continue;
				String[] codeVer = map.get("group").toString().split("_");
				if(map.containsKey("objName")) {
					details.setModelName(map.get("objName").toString());
				}else {
					details.setModelName(map.get("DecisionFlowName").toString());
				}
				details.setRuleName(map.get("name").toString());
				details.setRuleType(map.get("type").toString());
				details.setSalience(map.get("salience").toString());
				details.setProjectCode(ruleLogs.getProjectCode());
				details.setCode(codeVer[0]);
				//details.setVersion(codeVer[1]);
				details.setCreateDate(currDate);
				details.setRelationId(ruleLogs.getId());
				details.setField1(ruleLogs.getRuleflowid());
				ruleLogDetail.add(JSON.toJSONString(details));
			}
			redisService.lPush("rule_logs", ruleLogStr);
			redisService.lPushAll("rule_log_details",ruleLogDetail);
		}catch(Exception e) {
			com.tansun.ruledata.data.entity.RuleDeployLogDetails ruleLogDetails  = new com.tansun.ruledata.data.entity.RuleDeployLogDetails();
			ruleLogDetails.setRuleLogs(JSON.toJSONString(ruleLogs));
			ruleLogDetails.setRuleLogDetails(JSON.toJSONString(ruleLogDetail));
            ruleLogDetails.setIssynchronism("0");
			ruleLogDetails.setCreateDate(new Date());
			this.ruleLogService.saveLogToBackup(ruleLogDetails);
		}
		
	}
	
	public Map<String,Object> parseRuleLog(DroolsRuleLog droolsRuleLog,Map<String,Object> retMap,String projectCode){
		if(retMap==null) {
			retMap = new HashMap<>();
		}
		Map<String,Object> systemOut = (Map<String, Object>) retMap.get("RuleOutInfo");
		if(systemOut==null) {
			systemOut = new HashMap<>();
			retMap.put("RuleOutInfo", systemOut);
		}
		Object obj = retMap.get("ProcessingInfo");
		List<Map<String, Object>> ProcessingInfo = null;
		if(obj!=null) {
			ProcessingInfo =(List<Map<String, Object>>) obj;
			ProcessingInfo.addAll(droolsRuleLog.getLogs());
		}else {
			ProcessingInfo = droolsRuleLog.getLogs();
		}
		Map<String,Object> ResponseInfo =new HashMap<>();
		List<String> AllReasonCode = (List<String>) ResponseInfo.get("AllReasonCode");
		List<String> AllRuleScore = (List<String>) ResponseInfo.get("AllRuleScore");
		List<String> AllRuleList = (List<String>) ResponseInfo.get("AllRuleList");
		List<String> AllRuleEntry = (List<String>) ResponseInfo.get("AllRuleEntry");
		if(AllReasonCode==null) {
			AllReasonCode = new ArrayList<>();
			ResponseInfo.put("AllReasonCode", AllReasonCode);
		}
		if(AllRuleScore==null) { AllRuleScore = new ArrayList<>();ResponseInfo.put("AllRuleScore", AllRuleScore);}
		if(AllRuleList==null) {AllRuleList = new ArrayList<>();ResponseInfo.put("AllRuleList", AllRuleList);}
		if(AllRuleEntry==null) {AllRuleEntry = new ArrayList<>();ResponseInfo.put("AllRuleEntry", AllRuleEntry);}
		
		
		List<String> tmp0 = new ArrayList<>();
		List<String> tmp1 = new ArrayList<>();
		List<String> tmp2 = new ArrayList<>();
		List<String> tmp3 = new ArrayList<>();
		for (Map<String, Object> map : droolsRuleLog.getLogs()) {
			String type = map.get("type").toString();
			if("policyrulegroup".equals(type)) {
				addToTmp(tmp0,map.get("info0"));
				addToTmp(tmp1,map.get("info1"));
				addToTmp(tmp2,map.get("info2"));
				addToTmp(tmp3,map.get("info3"));
			}
		}
		AllRuleEntry.addAll(tmp0);
		AllRuleList.addAll(tmp1);
		AllRuleScore.addAll(tmp2);
		AllReasonCode.addAll(tmp3);
		
		systemOut.put("ProjectCode", projectCode);
		systemOut.put("ResponseInfo", ResponseInfo);
		systemOut.put("ProcessingInfo", ProcessingInfo);
		return retMap;
	}
	private void addToTmp(List<String> tmpList,Object value) {
		if(!tmpList.contains(value) && value!=null && !"null".equals(value)) {
			tmpList.add(value.toString());
		}
	}
}
